package com.ruoyi.framework.security.filter.ykz;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.core.authority.mapping.NullAuthoritiesMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 处理渝快政系统的登录认证逻辑，包括从外部服务获取用户信息、加载用户详细信息、创建认证成功的 Authentication 对象等功能
 */
@Slf4j
@Component
public class YKZAuthenticationProvider implements AuthenticationProvider {
	@Autowired
	private IntelligentPostService intelligentPostService;

	@Autowired
	private SysUserMapper userMapper;
	
	private final static GrantedAuthoritiesMapper authoritiesMapper = new NullAuthoritiesMapper();
	
	@Override
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		log.info("authentication:" + authentication);
		YKZAuthenticationToken token = (YKZAuthenticationToken) authentication;
		log.info("token:" + token);
		String ykzToken = intelligentPostService.getToken();
		// 渝快政登录请求带过来的code
		String authCode = token.getCode();
		OapiRpcAuth2UserInfo userInfo = intelligentPostService.getUserInfo(authCode, ykzToken);
		log.info("渝快政userInfo:{}", userInfo);
		if (userInfo == null) {
			throw new RuntimeException(authCode + ", 用户不存在");
		}
		Long accountId = userInfo.getAccountId();
		log.info("愉快政用户accountId:{}", accountId);
		// 根据accountId获取系统用户
		SysUser user = null;
		user = userMapper.selectUserByAccountId(String.valueOf(accountId));
		log.info("SysUser:{}", user);
		if (Objects.isNull(user)) {
//			// 系统用户不存在 插入用户
//			user = new SysUser();
//			user.setAccountId(String.valueOf(accountId));
//			user.setUserName(userInfo.getLastName());
//			user.setNickName(userInfo.getNickNameCn());
//			user.setUserType("2");
//			int i = userMapper.insertUser(user);
//			log.info("插入SysUser成功:{}", user);
			log.warn("SysUser is null,accountId为:{}", accountId);
		    return null;
		}
		token.setCredentials(user);		
		CustomUserDetails userDetails = new CustomUserDetails(user);
		return createSuccessAuthentication(user, authentication, userDetails);
	}

	protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) {
		YKZAuthenticationToken result = new YKZAuthenticationToken(principal, authentication.getCredentials(), authoritiesMapper.mapAuthorities(user.getAuthorities()));
        result.setDetails(authentication.getDetails());
        return result;
    }
	
	@Override
	public boolean supports(Class<?> authentication) {
		return YKZAuthenticationToken.class.isAssignableFrom(authentication);
	}

}
